<?php

namespace App\Models;

use App\Models\Traits\Timestamp;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory,Timestamp;

    protected $fillable = [
        'id','name','preview','images','price','integral','increase','pick','transfer','user_id','status'
    ];

    protected $casts = [
        'images'    => 'array',
    ];

    public function user()
    {
        return $this->belongsTo(AppUser::class,'user_id');
    }

    public function order()
    {
        return $this->hasMany(Order::class,'product_id')->latest();
    }

    protected static function booted()
    {
        static::created(function ($model){

            $setting = SystemSeting::find('product')->data;

            $flag = Product::find($model->id);
            $flag->integral = round($flag->price * $setting['integral'] / 100,2);
            $flag->increase = round($flag->price * $setting['increase'] / 100,2);
            $flag->save();

        });
    }
}
